package dbConnect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class JdbcDbUpdateExample {
	private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:xe";
	private static final String DB_USER = "ntg";
	private static final String DB_PASSWORD = "ntg123";

	public static void main(String[] args){
		try{
			//createTable();
			InsertRecordsIntoTable();
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
	}
	public static void createTable() throws SQLException{
		Connection dbConnection = null;
		Statement statement = null;
		String tableName = "EMP2";
		
		StringBuffer strSQL1 = new StringBuffer();
		
		strSQL1.append("CREATE TABLE " + tableName + " (ENAME VARCHAR(10), EMPNO VARCHAR(10))");
		
		System.out.println("SQL Order : " + strSQL1.toString());
		
		try{
			dbConnection = getDBConnection();
			statement = dbConnection.createStatement();
			
			statement.addBatch(strSQL1.toString());
			statement.executeBatch();
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}finally{
			if(statement != null){
				statement.close();
			}
			if(dbConnection != null){
				dbConnection.close();
			}
		}
	}

	public static void InsertRecordsIntoTable() throws SQLException{
		Connection dbConnection = null;
		Statement statement = null;

		StringBuffer strSQL1 = new StringBuffer();

		strSQL1.append("UPDATE EMP2 SET ENAME = 'AAA' where EMPNO = '111'");

		System.out.println("SQL Order : " + strSQL1.toString());

		try{
			dbConnection = getDBConnection();
			statement = dbConnection.createStatement();

			statement.addBatch(strSQL1.toString());
			statement.executeBatch();

			System.out.println("EMP2 테이브을 업데이트 하였습니다.");
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}finally{
			if(statement != null){
				statement.close();
			}
			if(dbConnection != null){
				dbConnection.close();
			}
		}
	}

	public static Connection getDBConnection(){
		Connection dbConnection = null;
		
		try{
			Class.forName(DB_DRIVER);
		}catch(ClassNotFoundException e){
			System.out.println(e.getMessage());
		}

		try{
			dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
			return dbConnection;
		}catch(SQLException e) {
			System.out.println(e.getMessage());
		}

		return dbConnection;
	}
}

